package com.romotc.qqrobot.webqq.qqInterfaceCatching;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;

import org.json.JSONException;

import com.romotc.qqrobot.bean.QQData;
import com.romotc.qqrobot.bean.QQDataService;
import com.romotc.qqrobot.webqq.WebQQConstant;
import com.romotc.scache.RequestSpy;
import com.romotc.scache.WebConnection;

public class DefaultUrl_Catching extends QQUrlBodyCatching {

	@Override
	public boolean checkChatchingCondition(RequestSpy requestSpy) {
		return super.checkChatchingCondition(requestSpy);
	}

	@Override
	String getHackReqBodyStr(RequestSpy requestSpy, Map<String, Object> var) {
		String loginUin = requestSpy.getReqCookiesJson().optString(WebQQConstant.Cookie_ptui_loginuin);
		
		if(WebQQConstant.isRobotUin(loginUin)) {
			//拦截未设置拦截器的webqqRobot请求
			
			//从qq_datas表中获取模板 或最新的一次请求
			QQData defaultResData = QQDataService.qryLatestQQDataTemplet(requestSpy.getURL(), true);
			if(defaultResData != null) {
				System.err.println("从数据库取返回数据");
				return defaultResData.getResult();
			}
				
			
			return "webQQRobot";
		}
		return null;
	}

	@Override
	boolean isGoOn(RequestSpy requestSpy) {
		return false;
	}

	@Override
	ArrayList<String> hackRequestHeader(RequestSpy requestSpy, int bodySize, Map<String, Object> var) {
		ArrayList<String> headers = requestSpy.headers;
		
		//打个标记
		headers.add("dataFrom: dbDefault");
		
		return headers;
	}

	@Override
	public boolean modifyResponse(WebConnection wc, RequestSpy rqSpy)
			throws IOException {
		return false;
		
	}

}
